package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppinghCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
//    /**
//     * 添加购物车
//     * @param shoppingCart
//     * @return
//     */
//
//    @PostMapping("/add")
//    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
//        log.info("添加的菜品内容为:{}", shoppingCart);
//        //设置用户id，知道是那个用户添加的购物车
//        Long currentId = BaseContext.getCurrentId();
//        shoppingCart.setUserId(currentId);
//        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(ShoppingCart::getUserId, currentId);
//        //获取传入的菜品的id
//        Long dishId = shoppingCart.getDishId();
//        //如果传入的菜品id不为空，则添加到购物车的是菜品，否则添加到购物车的是套餐
//        if (dishId != null) {
//            lambdaQueryWrapper.eq(ShoppingCart ::getDishId,shoppingCart.getDishId());
//        } else {
//            lambdaQueryWrapper.eq(ShoppingCart ::getSetmealId,shoppingCart.getSetmealId());
//        }
//        //查询当前菜品或者套餐是否在购物车中
//        ShoppingCart shoppingCart1 = shoppingCartService.getOne(lambdaQueryWrapper);
//        if (shoppingCart1 != null) {
//            //如果已经存在，就在原来数量基础上加一
//            Integer number = shoppingCart1.getNumber();
//            shoppingCart1.setNumber(number+1);
//            shoppingCartService.updateById(shoppingCart1);
//        }else {
//         //如果不存在，则添加到购物车，数量默认就是一
//            shoppingCart.setNumber(1);
//            shoppingCart.setCreateTime(LocalDateTime.now());
//            shoppingCartService.save(shoppingCart);
//            shoppingCart1 = shoppingCart;//test
//        }
//
//        return R.success(shoppingCart1);
//    }

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //设置完用户的id后就该判断传入的是菜品还是套餐了
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);
        //获取传入的套餐id进行判断
        Long setmealId = shoppingCart.getSetmealId();
        if (setmealId != null) {
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        } else {
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }
        //接下来判断购物车是否有这个菜品，有的话就在原基础上+1，没的话就设置为1
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if (one == null) {
            //等于空就设置为1
            shoppingCart.setNumber(1);
            //设置添加操作的时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            //设置完进行保存操作
            shoppingCartService.save(shoppingCart);
            //把保存完的数据赋上面的查询一天条后的变量值
            one = shoppingCart;
        } else {
            //不等于空就进行更新操作，在原来的基础上点一次增加一次,此时就需要用到getOne的返回One的数据进行操作了
            Integer number = one.getNumber();
            //设置数量+1
            one.setNumber(number + 1);
            //设置完进行根据id进行更新操作
            shoppingCartService.updateById(one);
        }
        return R.success(one);
    }


    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {//index传值为空 所以就不用传参数了
        log.info("购物车的内容为....");
        //根据用户的id查询就可以了，查询要升序
        Long currentId = BaseContext.getCurrentId();
        //设置查询条件
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //select * from ShoppingCart where userId=#{userId}
        queryWrapper.eq(ShoppingCart ::getUserId,currentId);
        //查询后的结结果按照升序的时间进行排序
        queryWrapper.orderByAsc(ShoppingCart ::getCreateTime);
        //把查询到的数据因为有可能是多条的所以返回到list里存放
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    @DeleteMapping("clean")
    public R<String> clean(){
         //获取用户的id
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //delete * from ShoppingCart where userId=#{userId}
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        shoppingCartService.remove(queryWrapper);

        return R.success("清空成功!");
    }


}
